﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Windows;

namespace CtxPublishTool
{
    [XmlRoot(ElementName = "config")]
    public class Configuration
    {
        [XmlElement(ElementName = "cms6AppPath")]
        public string CMS6AppPath { get; set; }
        [XmlElement(ElementName = "cms6PublishPath")]
        public string CMS6PublishPath { get; set; }
        [XmlElement(ElementName = "vcmsAppPath")]
        public string VCMSAppPath { get; set; }
        [XmlElement(ElementName = "vcmsPublishPath")]
        public string VCMSPublishPath { get; set; }
        [XmlElement(ElementName = "componentsAppPath")]
        public string ComponentsAppPath { get; set; }
        [XmlElement(ElementName = "componentsPublishPath")]
        public string ComponentsPublishPath { get; set; }

        [XmlElement(ElementName = "goldFtpPath")]
        public string GoldFtpPath { get; set; }

        [XmlArray("publishSites")]
        [XmlArrayItem("publishSite")]
        public PublishSite[] PublishSites { get; set; }

        public static readonly Configuration Current = null;
        static Configuration()
        {
            var xmlFilePath = Path.Combine(Environment.CurrentDirectory, "config.xml");
            using (var fileStream = new FileStream(xmlFilePath, FileMode.Open))
            {
                var xmlSerializer = new XmlSerializer(typeof(Configuration));
                Current = xmlSerializer.Deserialize(fileStream) as Configuration;
            }
            if (Current != null)
            {
                Current.CMS6AppPath = Current.CMS6AppPath.TrimEnd('/');
                Current.CMS6PublishPath = Current.CMS6PublishPath.TrimEnd('/');
                Current.ComponentsAppPath = Current.ComponentsAppPath.TrimEnd('/');
                Current.ComponentsPublishPath = Current.ComponentsPublishPath.TrimEnd('/');
                Current.GoldFtpPath = Current.GoldFtpPath.TrimEnd('/');
                Current.VCMSAppPath = Current.VCMSAppPath.TrimEnd('/');
                Current.VCMSPublishPath = Current.VCMSPublishPath.TrimEnd('/');
            }
        }
    }

    public class PublishSite
    {
        public PublishSite()
        {
            DeleteExists = true;
        }

        [XmlElement(ElementName="cms6SiteRootPath")]
        public string Cms6SiteRootPath { get; set; }

        [XmlElement(ElementName = "vcmsSiteRootPath")]
        public string VcmsSiteRootPath { get; set; }

        [XmlElement(ElementName = "cms6ConnectionString")]
        public string Cms6ConnectionString { get; set; }

        [XmlElement(ElementName = "vcmsConnectionString")]
        public string VcmsConnectionString { get; set; }

        [XmlElement(ElementName="deleteExists")]
        public bool DeleteExists { get; set; }
    }
}
